<?php
/**
 * @author bbernath
 *         Date: 10.11.13
 *         Time: 22:41
 * @version 1.0.1
 */

echo "<h3>&raquo; News bearbeiten</h3>";
echo "&laquo; <a href=index.php?action=administration&site=news&userid=" . $_GET['userid'] . ">Zur&uuml;ck zu News verwalten</a><br><br>";

if (isset($_GET['action'], $_GET['site'], $_GET['id'])) {
    if ($_GET['action'] == 'administration' && $_GET['site'] == 'edit_news' && isset($_GET['news'])) {
        $update = "update news set topic=?, content=? where id=?";
        $stmt = $db->prepare($update);
        $newsid = $_GET['id'];
        $newstext = str_replace("\n", "<br>", $_POST['newstext']);
        $stmt->bind_param("ssi", $_POST['topic'], $newstext, $newsid);
        $stmt->execute();
        $stmt->close();
        echo "Die News wurden erfolgreich bearbeitet.<br><br>";
    } else if ($_GET['action'] == 'administration' && $_GET['site'] == 'edit_news') {
        $sql = "select topic, content from news where id=?";
        $stmt = $db->prepare($sql);
        $stmt->bind_param("i", $_GET['id']);
        $stmt->execute();
        $stmt->bind_result($topic, $content);
        $stmt->fetch();
        ?>

        <form
            action='index.php?action=administration&site=edit_news&id=<?php echo $_GET['id'] ?>&news=update&userid=<?php echo $_GET['userid'] ?>'
            method='post'>
            <table id="news">
                <tr>
                    <td><b>&Uumlberschrift:</b></td>
                    <td><input name='topic' type='text' size='50' maxlength='50' value="<?php echo $topic ?>">
                    </td>
                </tr>
                <tr>
                    <td><b>Text:</b></td>
                    <?php $newstext = str_replace("<br>", "\n", $content) ?>
                    <td><textarea name='newstext' cols='70' rows='18'><?php echo $newstext ?></textarea></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type='submit' value='News bearbeiten'></td>
                </tr>
            </table>
        </form>
        <?php
        $stmt->close();
    } else {
        echo "Fehler beim Verarbeiten der Daten.";
    }
} else {
    echo "Fehler beim Verarbeiten der Daten.";
}
?>